﻿using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.MC
{
    /// <summary>
    /// 在线时长数据访问类
    /// </summary>
    public class OnlineTimeDal
    {
        /// <summary>
        /// 新增在线时长
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int AddOnlineTime(MC_OnlineTime obj)
        {
            string sql = "insert into MC_OnlineTime(UserID,TradeDate,Minutes,UpdateTime) values(@UserID,@TradeDate,@Minutes,getdate())";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, obj.UserID);
            tools.AddSqlParameter("TradeDate", System.Data.SqlDbType.DateTime, obj.TradeDate);
            tools.AddSqlParameter("Minutes", System.Data.SqlDbType.Int, obj.Minutes);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 修改在线时长
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int UpdateOnlineTime(MC_OnlineTime obj)
        {
            string sql = "update MC_OnlineTime set Minutes=@Minutes,UpdateTime=getdate() where UserID=@UserID and TradeDate=@TradeDate";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, obj.UserID);
            tools.AddSqlParameter("TradeDate", System.Data.SqlDbType.DateTime, obj.TradeDate);
            tools.AddSqlParameter("Minutes", System.Data.SqlDbType.Int, obj.Minutes);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取在线时长
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="tradeDate"></param>
        /// <returns></returns>
        public MC_OnlineTime GetOnlineTime(int userid, DateTime tradeDate)
        {
            string sql = "select * from MC_OnlineTime where UserID=@UserID and TradeDate=@TradeDate";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, userid);
            tools.AddSqlParameter("TradeDate", System.Data.SqlDbType.DateTime, tradeDate);

            return SQLHelper.GetDataByColumns<MC_OnlineTime>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }
    }
}
